
package com.thinkgem.jeesite.common.component.redis.element;

import java.util.List;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Response;
import redis.clients.jedis.Transaction;

/** 
 * 放置rds的管道类元素操作
 *
 */
public class RdsPipeline{
	
	public RdsPipeline(Jedis jedis) {
	    super();
	    this.jedis = jedis;
    }

	public Jedis getJedis() {
    	return jedis;
    }

	public void setJedis(Jedis jedis) {
    	this.jedis = jedis;
    }

	public static void pipeline(Jedis jedis){
		String key = "pipeline";
		String old = jedis.get(key);
		if(old != null){
			System.out.println("Key:" + key + ",old value:" + old);
		}
		//代码模式1,这种模式是最常见的方式
		Pipeline p1 = jedis.pipelined();
		p1.incr(key);
		System.out.println("Request incr");
		p1.incr(key);
		System.out.println("Request incr");
		//结束pipeline，并开始从相应中获得数据
		List<Object> responses = p1.syncAndReturnAll();
		if(responses == null || responses.isEmpty()){
			throw new RuntimeException("Pipeline error: no response...");
		}
		for(Object resp : responses){
			System.out.println("Response:" + resp.toString());//注意，此处resp的类型为Long
		}
		//代码模式2
		Pipeline p2 = jedis.pipelined();
		Response<Long> r1 = p2.incr(key);
		try{
			r1.get();
		}catch(Exception e){
			System.out.println("Error,you cant get() before sync,because IO of response hasn't begin..");
		}
		Response<Long> r2 = p2.incr(key);
		p2.sync();
		try{
			r2.get();
		}catch(Exception e){
			System.out.println("io not start..");
		}
		System.out.println("Pipeline,mode 2,--->" + r1.get());
		System.out.println("Pipeline,mode 2,--->" + r2.get());
		
	}
	
	public static void multiPipeline(Jedis jedis){
		String key = "txptest";
		String old = jedis.get(key);
		jedis.set("txptest", "1");
		if(old != null){
			System.out.println("Key:" + key + ",old value:" + old);
		}
		//代码模式1,这种模式是最常见的方式
		Pipeline p1 = jedis.pipelined();
		p1.multi();
		p1.incr(key);
		System.out.println("Request incr");
		p1.incr(key);
		System.out.println("Request incr");
		//System.out.println(jedis.get(key));
		try {
			jedis.set("txptest", "1");
        } catch (Exception e) {
        	System.out.println("Error,you cant get() before sync,because IO of response hasn't begin..");
        }
		System.out.println("Request incr");
		Response<List<Object>> txresult= p1.exec();
		p1.sync();
		//结束pipeline，并开始从相应中获得数据
		List<Object> responses = txresult.get();
		if(responses == null || responses.isEmpty()){
			throw new RuntimeException("Pipeline error: no response...");
		}
		for(Object resp : responses){
			System.out.println("Response:" + resp.toString());//注意，此处resp的类型为Long
		}
	}
	
	public static void transaction(Jedis jedis){
		String key = "transaction";
		jedis.set(key, "20");
		jedis.watch(key);
		Transaction tx = jedis.multi();
		tx.incr(key);
		tx.incr(key);
		tx.incr(key);
		List<Object> result = tx.exec();
		if(result == null || result.isEmpty()){
			System.out.println("Transaction error...");//可能是watch-key被外部修改，或者是数据操作被驳回
			return;
		}
		for(Object rt : result){
			System.out.println(rt.toString());
		}
	}
	
	/**
	 * 测试当前连接客户端的pipeline信息
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
//		RedisClient client = new RedisClient(Constants.host, Constants.port);
//		RdsMasterLink client = new RdsMasterLink();
//		pipeline(client.getJedis());
//		multiPipeline(client.getJedis());
//		transaction(client.getJedis());
	}

	private Jedis jedis;//当前客户端
}
